import { checkArg } from "../utils/check";
import { DBMgr } from "./db";
import { INVALID_PARAMETER, INVALID_PASSWORD, NOT_FOUND_ACCOUNT } from "./err";


export class UserModule{
    static ins:UserModule;

    async updateMemberList(argv:{member:string[]}){
        
    }

    //获取分会会员行业表
    async loadAgencyMemberList(argv:{chapter:string}){
        let members = await DBMgr.ins.prisma.t_user.findMany({
            where:{chapter:argv.chapter}
        });

        let unameList = members&&members.map((item)=>{
            return item.uname
        })

        let sunchats = await DBMgr.ins.prisma.t_sunchart.findMany({
            where:{uname:{in:unameList},removed:0}
        })

        return sunchats.map((item)=>{
            item.val1
        });
    }

    //用户登录
    async loginByName(argv:{uname:string,chapter:string}){
        
        let checkRes = checkArg(argv,"uname","chapter")
        if(checkRes){
            return INVALID_PARAMETER;
        }
        let user = await DBMgr.ins.prisma.t_user.findFirst({
            where:{uname:argv.uname,chapter:argv.chapter}
        })

        if(!user){
            let newUser = {
                uname:argv.uname,
                chapter:argv.chapter
            }

            await DBMgr.ins.prisma.t_user.create({
                data:newUser
            });
        }

        return user;
    }

        //用户密码登录
        async loginWithPassword(argv:{uname:string,chapter:string,password:string}){
        
            let checkRes = checkArg(argv,"uname","chapter","password")
            if(checkRes){
                return INVALID_PARAMETER;
            }

            let user = await DBMgr.ins.prisma.t_user.findFirst({
                where:{uname:argv.uname,chapter:argv.chapter}
            })
    
            if(!user){
                return NOT_FOUND_ACCOUNT; 
            }else if(user.password != argv.password){
                return INVALID_PASSWORD;
            }
    
            return user;
        }


    //模糊搜行业关键词
    async findSimular(argv:{key:string}){
        let res = await DBMgr.ins.prisma.t_sunchart.findMany({
            where:{ "OR":[
                {val2:{"contains":argv.key}},
                {val3:{"contains":argv.key}},
                {val4:{"contains":argv.key}},
                {val5:{"contains":argv.key}},
            ]},
            select: {
                uname: true,  // 仅选择 uname 字段
                val1: true
            }
        });

        return res;
    }

    
    // 用户注册
    async register(argv: { uname: string, password: string, chapter: string, email: string }) {
        let checkRes = checkArg(argv, "uname", "password", "chapter", "email")
        if (checkRes) {
            return INVALID_PARAMETER;
        }

        // 检查用户是否已存在
        const existingUser = await DBMgr.ins.prisma.t_user.findFirst({
            where: { uname: argv.uname }
        });

        if (existingUser) {
            return { code: -1, msg: "用户名已存在" };
        }

        // 创建新用户
        const newUser = await DBMgr.ins.prisma.t_user.create({
            data: {
                uname: argv.uname,
                password: argv.password, // 注意：实际使用时需要对密码进行加密
                chapter: argv.chapter,
                email: argv.email
            }
        });

        return { code: 0, data: newUser };
    }

    // 密码登录
    async login(argv: { uname: string, password: string }) {
        let checkRes = checkArg(argv, "uname", "password")
        if (checkRes) {
            return INVALID_PARAMETER;
        }

        const user = await DBMgr.ins.prisma.t_user.findFirst({
            where: {
                uname: argv.uname,
                password: argv.password // 注意：实际使用时需要对密码进行加密比对
            }
        });

        if (!user) {
            return { code: -1, msg: "用户名或密码错误" };
        }

        return { code: 0, data: user };
    }

    // 忘记密码
    async forgotPassword(argv: { uname: string, email: string }) {
        let checkRes = checkArg(argv, "uname", "email");
        if (checkRes) {
            return INVALID_PARAMETER;
        }

        // 验证用户和邮箱是否匹配
        const user = await DBMgr.ins.prisma.t_user.findFirst({
            where: {
                uname: argv.uname,
                email: argv.email
            }
        });

        if (!user) {
            return { code: -1, msg: "用户名或邮箱不正确" };
        }

        // 这里可以添加发送重置密码邮件的逻辑
        // TODO
        // 例如，生成一个重置密码的链接并发送到用户的邮箱

        return { code: 0, msg: "重置密码邮件已发送" };
    }

    // 重置密码
    async resetPassword(argv: { uname: string, email: string, newPassword: string }) {
        let checkRes = checkArg(argv, "uname", "email", "newPassword")
        if (checkRes) {
            return INVALID_PARAMETER;
        }

        // 验证用户和邮箱是否匹配
        const user = await DBMgr.ins.prisma.t_user.findFirst({
            where: {
                uname: argv.uname,
                email: argv.email
            }
        });

        if (!user) {
            return { code: -1, msg: "用户名或邮箱不正确" };
        }

        // 更新密码
        await DBMgr.ins.prisma.t_user.update({
            where: { id: user.id },
            data: { password: argv.newPassword } // 注意：实际使用时需要对密码进行加密
        });

        return { code: 0, msg: "密码重置成功" };
    }

}

UserModule.ins = new UserModule()